Tables

Using Rules_Tables Instead of Rule_Sets

samples/basic/tutorial/loan.kb

In some rule_sets you might find that you are repeating the same conditions over and over again. For example, if you are calculating how much money to loan for the purchase of a house, you might have the following rule_set named max_loan_amount:

conditions
value
income >= 50000 and debt < 10000 and rating = "poor"
75000
income >= 50000 and debt < 10000 and rating = "good"
150000
income > 35000 and debt < 5000 and rating = "poor"
50000
income > 35000 and debt < 5000 and rating = "good"
100000

A more readable way to represent this would be a table.

We'll create a rules_table as part of a knowledgebase that determines the maximum loan amount based on various factors.

  1. Create a new knowledgebase named loan.
  2. Set the goal of the main knowledgebase object to max_loan_amount.
  3. Create the following three questions:
  4. Create a rules_table named max_loan_amount. Note that the column max_loan_amount has been created for you. The values in this column are the ones the object max_loan_amount may take on.
  5. Using the Column/Add command, add columns for income, debt and rating. Make sure the max_loan_amount column is last. You can change order of columns by dragging the column heading.
  6. Using the Row/Add command create the following table:
    income
    debt
    rating
    max_loan_amount
    > 50000
    < 10000
    "poor"
    75000
    > 50000
    < 10000
    "good"
    150000
    > 35000
    < 5000
    "poor"
    50000
    > 35000
    < 5000
    "good"
    100000

    Notice how this multi-column approach is easier to read than long conditions with lots of 'ands' in them.
  7. Run it, note that the order of the rows in the table assures that someone with income over 50000 does not get the over 35000 max_loan_amount.
  8. When using tables, it is a good idea to add a last "catch all" row to the table so that a value is returned. Add the following row onto the end of the rules_table max_loan_amount:
    income
    debt
    rating
    max_loan_amount
    *
    0
    The asterisks mean "match any value". So if none of the rows above match, the last one will, returning a max_loan_amount of 0. You can test this by entering an income of 20000 with debt of 10000 and a rating of 'poor'.

Copyright ©1996-2002 Amzi! inc. All Rights Reserved. Amzi!, KnowledgeWright and WebLS are registered trademarks and Subscription Plus and Logic Server are trademarks of Amzi! inc.